package com.realgodo.site.web.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import com.realgodo.site.entity.application.Application;
import com.realgodo.site.entity.relation.FriendConfig;
import com.realgodo.site.entity.relation.Relation;
import com.realgodo.site.entity.space.SpaceVisit;
import com.realgodo.site.service.application.ApplicationManager;
import com.realgodo.site.service.application.ShowManager;
import com.realgodo.site.service.relation.FriendConfigManager;
import com.realgodo.site.service.space.MessageManager;
import com.realgodo.site.service.space.SpaceCoverManager;
import com.realgodo.site.service.space.SpacePointManager;
import com.realgodo.site.service.space.SpaceVisitManager;

/**
 * 用户后台首页
 * 
 * @author biqiang E-mail: bijiqiang@gmail.com
 * @version 创建时间：2009-12-11下午10:11:08
 */
@Scope("prototype")
public class IndexController extends UserDefaultController {

	private static final long serialVersionUID = 6097398821486346742L;

	@Autowired
	private ApplicationManager applicationManager;

	@Autowired
	private SpaceVisitManager spaceVisitManager;

	@Autowired
	private FriendConfigManager friendCinfigManager;

	@Autowired
	private ShowManager showManager;

	@Autowired
	private SpaceCoverManager spaceCoverManager;

	@Autowired
	private SpacePointManager spacePointManager;

	@Autowired
	private MessageManager messageManager;

	private List<FriendConfig> friendConfigList;

	private Long showCount;
	private Long spaceCoverCount;
	private Long moodCount;

	private int n = 0;

	private String message;

	private Long spacePoint;

	private Long newFriendsCount;

	private Long newMessagesCount;
	
	private boolean avatarFlag;

	public String execute() {

		super.getParamMap().put("spaceId",
				super.getMySpace().getId().toString());
		super.getParamMap()
				.put("status", String.valueOf(Relation.STATUS_AGREE));
		friendConfigList = friendCinfigManager.find(super.getParamMap());
		showCount = showManager.getShowCount(super.getMySpace().getId());// 展示数量
		spaceCoverCount = this.spaceCoverManager.getShowCoverCount(super
				.getMySpace().getId());// 封面数量
		moodCount = this.moodManager.getMoodCount(super.getMySpace().getId());// 围脖数量
		newFriendsCount = this.friendManager.getNewFriendCount(getMySpace());
		newMessagesCount = this.messageManager.getNewMessageCount(getMySpace());
		avatarFlag = userManager.hasUpdateAvatar(getMySpace());
		if (n == 1) {// 加分
			if (this.spacePointManager
					.increasePoint(super.getMySpace().getId())) {
				message = "LOGINPOINT";
			}
			n = 0;
		}
		spacePoint = this.spacePointManager.getSpacePointBySpace(
				super.getMySpace().getId()).getPoint();
		return SUCCESS;
	}

	/*************** getter and setter ******************/
	/**
	 * 获取全部应该服务
	 * 
	 * @return
	 */
	public List<? extends Application> getApplications() {
		return this.applicationManager.getFriendAndMyApplications(super
				.getSpace().getId(), 20);
	}

	public List<SpaceVisit> getVisitors() {
		return (super.getSpace() != null) ? spaceVisitManager
				.getMySpaceVisitors(super.getSpace().getId(), 5) : null;
	}

	public List<FriendConfig> getFriendConfigList() {
		return friendConfigList;
	}

	public Long getShowCount() {
		return showCount;
	}

	public Long getSpaceCoverCount() {
		return spaceCoverCount;
	}

	public Long getMoodCount() {
		return moodCount;
	}

	public void setN(int n) {
		this.n = n;
	}

	public String getMessage() {
		return message;
	}

	public Long getSpacePoint() {
		return spacePoint;
	}

	public Long getNewFriendsCount() {
		return newFriendsCount;
	}

	public void setNewFriendsCount(Long newFriendsCount) {
		this.newFriendsCount = newFriendsCount;
	}

	public Long getNewMessagesCount() {
		return newMessagesCount;
	}

	public void setNewMessagesCount(Long newMessagesCount) {
		this.newMessagesCount = newMessagesCount;
	}

	public boolean isAvatarFlag() {
		return avatarFlag;
	}

	public void setAvatarFlag(boolean avatarFlag) {
		this.avatarFlag = avatarFlag;
	}

}
